package com.fenghongzhang.a1810;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private Button btn;

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        btn = (Button) findViewById(R.id.btn);
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//            }
//        });


    }

    public void btn(View view) {

        //创建一个对话框的构造者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        builder.setMessage("内容");//设置内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //构造者
        AlertDialog alertDialog = builder.create();

        alertDialog.show();
    }

    public void btn1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");

//        builder.setMessage("内容");

        final String[] s  = new String[]{"篮球","游戏","开发"};
        builder.setSingleChoiceItems(s, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //构造者
        AlertDialog alertDialog = builder.create();

        alertDialog.show();
    }

    public void btn2(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
//        builder.setMessage("内容");
//        final String[] s  = new String[]{"篮球","游戏","开发"};
//        builder.setSingleChoiceItems(s, 1, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
//            }
//        });
        final String[] s  = new String[]{"篮球","游戏","开发"};
        final boolean[] b = new boolean[]{true,true,false}; //不能包装类型

        //多选
        builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < b.length; i++) {
                    if(b[i]){
                        Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //构造者
        AlertDialog alertDialog = builder.create();

        alertDialog.show();
    }

    public void btn3(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
//        builder.setMessage("内容");
        View inflate = LayoutInflater.from(this).inflate(R.layout.alert_layout, null);
        builder.setView(inflate);

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        final AlertDialog alertDialog = builder.create();
        //把xml -> view
        ImageView imageView = inflate.findViewById(R.id.iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        alertDialog.show();


    }

    public void btn4(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("标题");
        progressDialog.setMax(100); //文件大小
        progressDialog.setProgress(10);
        progressDialog.setSecondaryProgress(20);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        //定时器
        Log.i(TAG, "btn4: ");
        final Timer timer = new Timer(); //闹钟
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                Log.i(TAG, "run: ");
                index+=10;
                progressDialog.show();
                progressDialog.setProgress(index);
                if (index >= 100){
                    timer.cancel();
                    progressDialog.dismiss();
                }
            }
        },3000,1000);

//        timer.cancel();

        progressDialog.show();

    }

    public void btn5(View view) {

        Calendar calendar = Calendar.getInstance();
//        calendar.get(Calendar.YEAR);//2020


        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            /**
             *
             * @param view
             * @param year
             * @param month 从0 到11
             * @param dayOfMonth
             */
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+"年"+(month+1)+"月"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

        //10 2 8 ,16 0x 08 09
        datePickerDialog.show();
    }

    public void btn6(View view) {
        Calendar calendar = Calendar.getInstance();
        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), false);

//        System.currentTimeMillis(); //当前系统的毫秒值 197011

        timePickerDialog.show();
    }

    public void btn7(View view) {

        MyDialog myDialog = new MyDialog(this);

//        myDialog.setMessage();
        //5,调用
        myDialog.setStrMsg("h哈哈哈");
        myDialog.setStrTi("标题踢踢踢踢");

        myDialog.setYesOnClick(new MyDialog.YesOnClick() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "哈哈", Toast.LENGTH_SHORT).show();
                Log.i(TAG, "click: 哈哈哈哈哈");
            }
        });
        
        myDialog.setNoOnClick(new MyDialog.NoOnClick() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.show();

    }
}
